using Microsoft.AspNetCore.Mvc;
using UniversalAdminSystem.Api.Attributes;
using UniversalAdminSystem.Application.AIQuestions.DTOs;
using UniversalAdminSystem.Application.AIQuestions.Interfaces;
using UniversalAdminSystem.Application.Common.Results;

namespace UniversalAdminSystem.Api.Controllers;

[ApiController]
[Route("api/[controller]")]
public class AIQuestionsController : ControllerBase
{
    private readonly IAIQusetionsAppService _AIQusetionAppService;

    public AIQuestionsController(IAIQusetionsAppService aIQusetionsAppService)
    {
        _AIQusetionAppService = aIQusetionsAppService;
    }

    [HttpPost("visitor")]
    public Task<IActionResult> VisitorAccess(ContentDto content)
    {
        throw new NotImplementedException();//尚未实现
    }

    [HttpPost("chat/{conversationsId}")]
    [RequirePermission("document:Read")]
    public async Task<IActionResult> SendMessageAsync(Guid conversationsId, [FromBody] string userMessage)
    {
        try
        {
            
            var result = await _AIQusetionAppService.Chat(conversationsId, userMessage);
            return Ok(Result.Success(result));
        }
        catch (System.Exception e)
        {
            return BadRequest(Result.Failure(e.Message));
        }
    }

    [HttpPost("user")]
    [RequirePermission("document:Read")]
    public async Task<IActionResult> CreateUserConversation(ConversationsDto conversationsDto)
    {
        try
        {
            var result = await _AIQusetionAppService.CreateConversation(conversationsDto);
            return Ok(Result<ConversationsResultDto>.Success(result));
        }
        catch (System.Exception e)
        {
            return BadRequest(Result.Failure(e.Message));
        }
    }

    [HttpGet("user/{userid}")]
    [RequirePermission("document:Read")]
    public async Task<IActionResult> GetUsersConversationsByUserId(Guid userid)
    {
        try
        {
            var list = await _AIQusetionAppService.GetUsersConversationsByUserId(userid);
            return Ok(Result<IEnumerable<ConversationsResultDto>>.Success(list));
        }
        catch (System.Exception e)
        {
            return BadRequest(Result.Failure(e.Message));
        }
    }

    [HttpDelete("user/{conversationsId}")]
    public async Task<IActionResult> DeleteUserConversations(Guid ConversationsId)
    {
        try
        {
            await _AIQusetionAppService.DeleteUserConversations(ConversationsId);
            return Ok(Result.Success());
        }
        catch (System.Exception e)
        {
            return BadRequest(Result.Failure(e.Message));
        }
    }

    [HttpGet("user/message/{ConversationsId}")]
    public async Task<IActionResult> GetAllConversationMessage(Guid ConversationsId)
    {
        try
        {
            var list = await _AIQusetionAppService.GetConversationMessage(ConversationsId);
            return Ok(Result<IEnumerable<ContentResultDto>>.Success(list));
        }
        catch (System.Exception e)
        {
            return BadRequest(Result.Failure(e.Message));
        }
    }
}